package capaDatos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import utiles.UIDGenerator;

import capaLogica.Noticia;

public class NoticiaDAO {
	
	/**
	 * Atributo que crea la conexion a la bd
	 */
	private Connection conn;
	
	/**
	 * Constructor de la clase
	 */
	public NoticiaDAO(){
		conn = (Connection) ConnectionManager.getInstance().checkOut();
	}
	
	@Override
	 protected void finalize() {
       ConnectionManager.getInstance().checkIn(conn);
	}
	
	 public Noticia recuperarNoticiaById(String idNoticia) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        Noticia p = null;
	        String sql = "SELECT * FROM noticia WHERE (idNoticia = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, idNoticia);
	            result = stmt.executeQuery();

	            if ( result.next() ){
		            p = new Noticia();
		            p.setIdNoticia(result.getString("idNoticia"));
		            p.setFechaPublicacion(result.getDate("fechaPublicacion"));
		            p.setIdUsuarioCreador(result.getString("idUsuarioCreador"));
		            p.setDescripcion(result.getString("descripcion"));
	            }
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p;
	    }
	 
	 public List<Noticia> recuperarTodas() {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        List<Noticia> p1 = null;
	        
	        String sql = "SELECT * FROM noticia";

	        try {
	            stmt = conn.prepareStatement(sql);
	            result = stmt.executeQuery();
	            p1 = new ArrayList<Noticia>();
	            while( result.next() ){
	            	Noticia p = new Noticia();
		            p.setIdNoticia(result.getString("idNoticia"));
		            p.setFechaPublicacion(result.getDate("fechaPublicacion"));
		            p.setIdUsuarioCreador(result.getString("idUsuarioCreador"));
		            p.setDescripcion(result.getString("descripcion"));
		            p1.add(p);
	            }
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p1;
	    }
	 
	 public void borrarNoticia(String idNoticia) {
			PreparedStatement stm = null;
			String sql = "DELETE  FROM noticia WHERE (idNoticia = ?)";
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1, idNoticia);
				stm.executeUpdate();
				
			} catch (SQLException e) {
				System.out.println("SQLMessage: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}	
	 
	 public Noticia insertarNoticia(Noticia n) {
			String sql = "INSERT INTO noticia (idNoticia,descripcion,fechaPublicacion,idUsuarioCreador) VALUES (?,?,?,?) ";
			PreparedStatement stmt = null;
			
			try {
				stmt = conn.prepareStatement(sql);
				n.setIdNoticia(UIDGenerator.getInstance().getKey());
				
				java.util.Date fechaNoticia= n.getFechaPublicacion();
	            
				stmt.setString(1, n.getIdNoticia());
				stmt.setString(2, n.getDescripcion());
				stmt.setDate(3, new java.sql.Date(fechaNoticia.getTime()));
				stmt.setString(4, n.getIdUsuarioCreador());
			
				stmt.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stmt != null) {
						stmt.close();
						
					}
				} catch (SQLException e) {
				}
			}
			
			return n;
		}
}
